package weaver.interfaces.hggd.zjs.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.doc.mobile.systemDoc.util.SystemDocUtil;
import com.wbi.util.Util;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.hrm.User;
import weaver.interfaces.hggd.zjs.Util.AMSUtil;
import weaver.interfaces.hggd.zjs.Util.LogUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.zijianshe
 * @version: 1.0
 * @CreateDate: 2023-10-24
 * @Description: 发文传对方系统
 **/
public class WeaFW_Action extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        JSONObject paramJson = new JSONObject();
        JSONObject data = new JSONObject();
        writeLog(">>>>>WeaFW_Action>>>>" + requestid);
        User user = requestInfo.getRequestManager().getUser();
        try {
            String workflowName = this.getNameByid("workflow_requestbase", "requestname", "requestid", requestid);
            new JSONObject();
            rs.executeQuery("select * from " + tablename + " where requestid=? ", new Object[]{requestid});
            rs.next();
            String ws = Util.null2String(rs.getString("ws"));
            String ngr = Util.null2String(rs.getString("ngr"));
            String ngrq = Util.null2String(rs.getString("ngrq"));
            String jjcd = Util.null2String(rs.getString("jjcd"));
            String zw = Util.null2String(rs.getString("zw"));
            String fs = Util.null2String(rs.getString("fs"));
            String fjkxz = Util.null2String(rs.getString("fjkxz"));
            String ngbm = Util.null2String(rs.getString("ngbm"));
            String departmentName = this.getNameByid("hrmdepartment", "departmentname", "id", ngbm);
            String ngrxm = this.getNameByid("hrmresource", "lastname", "id", ngr);
            String zwmc = this.getNameByid("docdetail", "docsubject", "id", zw);
            data.put("dataId", requestid);
            data.put("doctype", "发文");
            data.put("title", workflowName);
            data.put("author", ngrxm);
            data.put("authorld", ngr);
            data.put("docdate", ngrq);
            data.put("fwbm", departmentName);
            data.put("jjcd", jjcd);
            data.put("undertakedate", ngrq);
            data.put("wjfs", fs);
            data.put("textname", zwmc);
            JSONArray fileList = new JSONArray();
            String annexname = "";
            this.writeLog("附件id" + fjkxz);
            rs.executeQuery("select imagefileid,'url',imagefilename,filesize,imagefiletype from imagefile where imagefileid in (select imagefileid from docimagefile where docid in (" + fjkxz + "))", new Object[0]);

            JSONObject fileJSON;
            String message;
            String imagefilename;
            String ddcode;
            while (rs.next()) {
                fileJSON = new JSONObject();
                message = Util.null2String(rs.getString("imagefileid"));
                imagefilename = Util.null2String(rs.getString("imagefilename"));
                annexname = "".equals(annexname) ? imagefilename : annexname + "," + imagefilename;
                ddcode = this.getDDCode(user, message);
                fileJSON.put("fileId", message);
                fileJSON.put("filePath", "http://200.162.8.68:9000/weaver/weaver.file.FileDownload?fileid=" + message + "&download=1&ddcode=" + ddcode);
                fileJSON.put("fileName", imagefilename);
                fileJSON.put("fileSize", Util.null2String(rs.getString("filesize")));
                fileJSON.put("fileType", imagefilename.substring(imagefilename.lastIndexOf(".") + 1, imagefilename.length()));
                fileList.add(fileJSON);
            }
            rs.executeQuery("select imagefileid,'url',imagefilename,filesize,imagefiletype from imagefile where imagefileid in (select imagefileid from docimagefile where docid in (" + zw + "))", new Object[0]);
            writeLog("正文id" + zw);
            while (rs.next()) {
                fileJSON = new JSONObject();
                message = Util.null2String(rs.getString("imagefileid"));
                imagefilename = Util.null2String(rs.getString("imagefilename"));
                ddcode = getDDCode(user, message);
                fileJSON.put("fileId", message);
                fileJSON.put("filePath", "http://200.162.8.68:9000/weaver/weaver.file.FileDownload?fileid=" + message + "&download=1&ddcode=" + ddcode);
                fileJSON.put("fileName", imagefilename);
                fileJSON.put("fileSize", Util.null2String(rs.getString("filesize")));
                fileJSON.put("fileType", imagefilename.substring(imagefilename.lastIndexOf(".") + 1, imagefilename.length()));
                fileList.add(fileJSON);
            }

            data.put("annexname", annexname);
            data.put("fileList", fileList);
            paramJson.put("app", "文书");
            paramJson.put("workflowName", workflowName);
            paramJson.put("workflowId", requestid);
            paramJson.put("data", data);

            JSONObject ResponseJson;

            try {
                writeLog("发文流程接口参数：" + paramJson);
                String send = AMSUtil.send(paramJson.toString());
                LogUtil.InsertLog("", paramJson.toString(), send, "", requestid);
                ResponseJson = JSONObject.parseObject(send);
            } catch (Exception e) {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent("接口调用失败，请联系管理员");
                return Action.FAILURE_AND_CONTINUE;
            }
            String code = ResponseJson.getString("code");
            message = ResponseJson.getString("message");
            if (!"1".equals(code)) {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent("接口调用失败，失败原因：" + message);
                return Action.FAILURE_AND_CONTINUE;
            } else {
                return Action.SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("异常参考:" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    /**
     * 通过传的字段值查询 字段名
     *
     * @param tablename
     * @param queryName
     * @param id
     * @param value
     * @return
     */
    public String getNameByid(String tablename, String queryName, String id, String value) {
        if ("".equals(value)) {
            return "";
        }
        RecordSet rs = new RecordSet();
        rs.executeQuery("select " + queryName + " from " + tablename + " where " + id + "=?", value);
        if (rs.next()) {
            return weaver.general.Util.null2String(rs.getString(queryName));
        }
        return "";
    }

    /**
     * 获取附件免登录编码
     *
     * @param user
     * @param fileid
     * @return
     */
    public String getDDCode(User user, String fileid) {
        Map params = new HashMap();
        params.put("timelimit", "0");
        params.put("adminright", "1");
        String ddcode = SystemDocUtil.takeddcode(user, fileid, params);//该ddcode是有时效的默认是5分钟，如果不要时效控制的话，可以用下面的方式来传参
        return ddcode;
    }

}

